/**
 * 卡片选择算法测试
 * 测试改进后的逻辑：当已选择的卡片云豆足够支付时，不允许再选择其他卡片
 */

// 模拟卡片数据
const mockCards = [
  { id: 1, card_name: '福利卡A', card_score: 50, score: 50 },
  { id: 2, card_name: '福利卡B', card_score: 30, score: 30 },
  { id: 3, card_name: '福利卡C', card_score: 20, score: 20 },
  { id: 4, card_name: '福利卡D', card_score: 100, score: 100 }
];

// 模拟支付金额
const mockPayMoney = 80;

// 改进后的卡片选择算法
function improvedCardSelect(cardId, selectedCards, payMoney) {
  const card = mockCards.find(item => item.id == cardId);
  
  if (!card) {
    return { success: false, message: '卡片不存在' };
  }

  const newSelectedCards = [...selectedCards];
  const existingIndex = newSelectedCards.findIndex(item => item.id == cardId);

  if (existingIndex > -1) {
    // 如果已选中，则取消选择
    newSelectedCards.splice(existingIndex, 1);
    return { 
      success: true, 
      selectedCards: newSelectedCards, 
      message: '取消选择成功' 
    };
  } else {
    // 如果未选中，检查是否可以添加新卡片
    // 计算当前已选择卡片的云豆总量
    let currentTotalCardScore = 0;
    for (const selectedCard of newSelectedCards) {
      const cardScore = selectedCard.card_score || selectedCard.score || 0;
      currentTotalCardScore += cardScore;
    }

    // 如果当前已选择的卡片云豆已经足够支付，则不允许再选择其他卡片
    if (currentTotalCardScore >= payMoney) {
      return { 
        success: false, 
        message: '已选择的卡片云豆足够支付，请先取消选择' 
      };
    }

    // 可以添加新卡片
    newSelectedCards.push(card);
    return { 
      success: true, 
      selectedCards: newSelectedCards, 
      message: '选择成功' 
    };
  }
}

// 计算选中卡片的云豆总量
function calculateTotalCardScore(selectedCards) {
  let total = 0;
  for (const card of selectedCards) {
    total += card.card_score || card.score || 0;
  }
  return total;
}

// 测试用例
function runTests() {
  console.log('=== 卡片选择算法测试 ===\n');
  
  let selectedCards = [];
  
  // 测试用例1：选择第一张卡片
  console.log('测试1：选择福利卡A（50云豆）');
  let result = improvedCardSelect(1, selectedCards, mockPayMoney);
  console.log('结果:', result.message);
  if (result.success) {
    selectedCards = result.selectedCards;
    console.log('当前选中卡片云豆总量:', calculateTotalCardScore(selectedCards));
  }
  console.log('');

  // 测试用例2：选择第二张卡片
  console.log('测试2：选择福利卡B（30云豆）');
  result = improvedCardSelect(2, selectedCards, mockPayMoney);
  console.log('结果:', result.message);
  if (result.success) {
    selectedCards = result.selectedCards;
    console.log('当前选中卡片云豆总量:', calculateTotalCardScore(selectedCards));
  }
  console.log('');

  // 测试用例3：尝试选择第三张卡片（应该被阻止）
  console.log('测试3：尝试选择福利卡C（20云豆）- 应该被阻止');
  result = improvedCardSelect(3, selectedCards, mockPayMoney);
  console.log('结果:', result.message);
  console.log('当前选中卡片云豆总量:', calculateTotalCardScore(selectedCards));
  console.log('');

  // 测试用例4：取消选择第一张卡片
  console.log('测试4：取消选择福利卡A');
  result = improvedCardSelect(1, selectedCards, mockPayMoney);
  console.log('结果:', result.message);
  if (result.success) {
    selectedCards = result.selectedCards;
    console.log('当前选中卡片云豆总量:', calculateTotalCardScore(selectedCards));
  }
  console.log('');

  // 测试用例5：现在可以重新选择第三张卡片
  console.log('测试5：重新选择福利卡C（20云豆）');
  result = improvedCardSelect(3, selectedCards, mockPayMoney);
  console.log('结果:', result.message);
  if (result.success) {
    selectedCards = result.selectedCards;
    console.log('当前选中卡片云豆总量:', calculateTotalCardScore(selectedCards));
  }
  console.log('');

  // 测试用例6：选择一张大额卡片
  console.log('测试6：选择福利卡D（100云豆）');
  result = improvedCardSelect(4, selectedCards, mockPayMoney);
  console.log('结果:', result.message);
  if (result.success) {
    selectedCards = result.selectedCards;
    console.log('当前选中卡片云豆总量:', calculateTotalCardScore(selectedCards));
  }
  console.log('');

  // 测试用例7：尝试选择其他卡片（应该被阻止）
  console.log('测试7：尝试选择福利卡A - 应该被阻止');
  result = improvedCardSelect(1, selectedCards, mockPayMoney);
  console.log('结果:', result.message);
  console.log('当前选中卡片云豆总量:', calculateTotalCardScore(selectedCards));
  console.log('');

  console.log('=== 测试完成 ===');
}

// 运行测试
runTests(); 